<cfcomponent displayname="User Controller" extends="ModelGlue.unity.controller.Controller" output="false">
	<!--- 
		Any function set up to listen for the onRequestStart message happens before any of the <event-handlers>.
		This is a good place to put things like session defaults.
	--->
	<cffunction name="init" access="public" returntype="userscontroller" output="false">
		<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
		<cfargument name="instanceName" required="true" type="string" />
		<cfset var modelPath = "snipex.model" />
		<cfset var facadePath = "snipex.facades" />
		
		<cfset super.Init(arguments.ModelGlue) />
		<cfset variables.userFacade = createobject("component", facadePath & ".userFacade") />
		<cfset variables.usersService = getModelGlue().getConfigBean("usersService") />
		<cfset variables.libAccessService = getModelGlue().getConfigBean("libAccessService") />
		<cfset variables.reactor = getModelGlue().getConfigBean("ormService") />
		<cfset variables.udfs = createobject("component", modelPath & ".udfs") />

		<cfreturn this>		
	</cffunction>
	
	<cffunction name="Authenticate" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var isLoggedIn = false />
		<cfset var quser = false />
		<cfset var user = false />
		<cfset var passwordSalt =  variables.usersService.getpasswordSalt(
									arguments.event.getValue("snipexusername"))/>
		
		<cfset quser = variables.usersService.Authenticate(
											arguments.event.getValue("snipexusername")
											, hashPassword(arguments.event.getValue("snipexpassword") & passwordSalt)	
											) />
		
		<cfif quser.recordcount gt 0>
			<cfset user = variables.usersService.getByID(quser.userID) />
			
			<cfset variables.userFacade.setLoginStatus(true) />
			<cfset variables.userFacade.setSnipExSessionID(createUUID()) />
			<cfset variables.userFacade.setIsAdmin(user.getIsAdmin()) />
			<cfset variables.userFacade.setIsSysAdmin(user.getIsSysAdmin()) />
			<cfset variables.userFacade.setUserID(quser.userid) />		
			<cfset variables.userFacade.setUser(user) />
			<cfset isLoggedIn = true />
		</cfif>
		
		<cfif isLoggedIn eq false>
			<cfset arguments.event.addResult("notLoggedIn") />
		</cfif>
	</cffunction>
	
	<cffunction name="isLoggedIn" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
		<cfset getLogInStatus(arguments.event) />
		
		<cfif variables.userFacade.getLoginStatus()>
			<cfset arguments.event.addResult("loggedIn") />
		<cfelse>
			<cfset arguments.event.addResult("notLoggedIn") />
		</cfif>
	</cffunction>
	
	<cffunction name="getLogInStatus" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfif not variables.userFacade.hasID()>
			<cfset variables.userFacade.init() /> 
		</cfif>
		<cfset arguments.event.setValue("isLoggedIn",variables.userFacade.getLoginStatus()) />
	</cffunction>
	
	<cffunction name="isAdmin" access="public" returntype="boolean" output="false">
		<cfif variables.userFacade.getIsAdmin() is 1>
			<cfreturn true/>
		<cfelse>
			<cfreturn false/>
		</cfif>
	</cffunction>
	
	<cffunction name="getRoles" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
		<cfif isAdmin() and not isSysAdmin()>
			<cfset arguments.event.setValue("roles","admin") />		
		</cfif>
		
		<cfif isAdmin() and isSysAdmin()>
			<cfset arguments.event.setValue("roles","admin,sysadmin") />
		</cfif>
		
		<cfif not isAdmin() and not isSysAdmin()>
			<cfset arguments.event.setValue("roles","user") />
		</cfif>
		
		<cfif not isAdmin() and isSysAdmin()>
			<cfset arguments.event.setValue("roles","sysadmin") />
		</cfif>
	</cffunction>
	
	<cffunction name="adminAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfif not isAdmin() and not issysAdmin()>
			<cfset arguments.event.addResult("noaccess") />
		<cfelse>
			<cfset arguments.event.addResult("adminaccess") />
		</cfif>
		
	</cffunction>
	
	<cffunction name="sysAdminAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfif not isSysAdmin()>
			<cfset arguments.event.addResult("noaccess") />
		</cfif>
	</cffunction>
	
	<cffunction name="isSysAdmin" access="public" returntype="boolean" output="false">
		<cfif variables.userFacade.getIsSysAdmin() is 1>
			<cfreturn true/>
		<cfelse>
			<cfreturn false/>
		</cfif>
	</cffunction>
	
	<cffunction name="logout" access="public" returntype="void" output="false">	
		<cfargument name="event" type="any"/>
		<cfset variables.userFacade.init() />
		<cfset arguments.event.addResult("success")>
	</cffunction>
	
	<cffunction name="completeRegerstation" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="hashPassword" access="public" returntype="string" output="false">
		<cfargument name="password" type="string"/>
		<cfreturn variables.udfs.sha1(password) />
	</cffunction>
	
	<cffunction name="saveUser" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>		
		<cfset var userBean = variables.usersService.getByID(arguments.event.getValue("userid")) />
		
		<cfif arguments.event.getValue("userid") eq ''>
			<cfset userBean.setDTCreated(now()) />
			<cfset userBean.setIPCreated(cgi.REMOTE_ADDR) />
			<cfset userBean.setCreatedByID(variables.userFacade.getUserID()) />
			<cfset userBean.setCreatedByID(variables.userFacade.getUserID()) />
			<cfset userBean.setUserName(arguments.event.getValue("uName")) />
			<cfset userBean.setPasswordSalt(createUUID()) />			
			<cfset userBean.setPassword(hashPassword(arguments.event.getValue("pword") & userBean.getPasswordSalt())) />
		</cfif>
		
		<cfif arguments.event.ValueExists("newPassword")>
			<cfset userBean.setPasswordSalt(createUUID()) />
			<cfset userBean.setPassword(hashPassword(arguments.event.getValue("newpword") & userBean.getPasswordSalt())) />
		</cfif>
		
		<cfif arguments.event.ValueExists("email")>
			<cfset userBean.setEmail(arguments.event.getValue("email")) />
		</cfif>
		<cfif arguments.event.ValueExists("firstName")>
			<cfset userBean.setFirstName(arguments.event.getValue("firstName")) />
		</cfif>
		<cfif arguments.event.ValueExists("lastName")>
			<cfset userBean.setLastName(arguments.event.getValue("lastName")) />
		</cfif>
		<cfif arguments.event.ValueExists("isActive")>
			<cfset userBean.setisActive(arguments.event.getValue("isActive")) />
		</cfif>
		<cfif arguments.event.ValueExists("isSysAdmin")>
			<cfset userBean.setisSysAdmin(arguments.event.getValue("isSysAdmin")) />
		</cfif>
		<cfif arguments.event.ValueExists("isAdmin")>
			<cfset userBean.setisAdmin(arguments.event.getValue("isAdmin")) />
		</cfif>
		<cfif arguments.event.ValueExists("username")>
			<cfset userBean.setUsername(arguments.event.getValue("username")) />
		</cfif>
		
		<cfset userBean.setDTModified(now()) />
		<cfset userBean.setIPModified(cgi.REMOTE_ADDR) />
		<cfset userBean.setModifiedByID(variables.userFacade.getUserID()) />			
		<cfset userBean.setDTLastAccess(now()) />
		<cfset userBean.setIPLastAccess(cgi.REMOTE_ADDR) />
		
		<cfset variables.usersService.save(userBean) />
		<cfif isAdmin is false and isSysAdmin is false>
			<cfset arguments.event.setValue("msg","It worked!") />
			<cfset arguments.event.addResult("success") />
		<cfelse>
			<cfset arguments.event.setValue("msg","Profile save") />
			<cfset arguments.event.addResult("adminsuccess") />
		</cfif>
	</cffunction>
	
	<cffunction name="getUser" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset arguments.event.setValue("users",variables.usersService.getById(arguments.event.getValue("userid"))) />
	</cffunction>
	
	<cffunction name="getUserID" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>

		<cfif arguments.event.getValue("userid") eq ''>
			<cfif isAdmin() is false and isSysAdmin() is false>
				<cfset arguments.event.setValue("userid",variables.userFacade.getUserID()) />	
			</cfif>
		</cfif>
	</cffunction>
	
	<cffunction name="deleteUser" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var userBean = variables.usersService.getByID(arguments.event.getValue("userid")) />
		<cfset variables.usersService.remove(userBean) />
		<cfset arguments.event.addResult("sysadminsuccess") />
	</cffunction>
	
	<cffunction name="deleteLibraryAccessUserID" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset variables.libAccessService.deleteByUserID(arguments.event.getValue("userid")) />
		<cfset arguments.event.addResult("success") />
	</cffunction>
	
	<cffunction name="getUserNames" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="getAllUsers" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
		<cfset arguments.event.setValue("users",variables.usersService.getAll())>
	</cffunction>
	
</cfcomponent>